package com.moshang.blog.controller.back.system;

import com.moshang.blog.core.annotation.SystemLog;
import com.moshang.blog.core.utils.RestResponse;
import com.moshang.blog.entity.SysDept;
import com.moshang.blog.service.DeptService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @author xww
 * Created on 2019/7/2.
 */
@Controller
@RequestMapping("/back/system/dept")
public class DeptController {

    private final String prefix = "back/system/dept/";
    @Autowired
    private DeptService deptService;

    @GetMapping("list")
    @SystemLog("跳转部门列表页面")
    public String list() {
        return prefix + "list";
    }

    @GetMapping("/tree/root")
    @ResponseBody
    public RestResponse treeAndRoot() {
        return RestResponse.success().setData(deptService.selectAllDeptTreeAndRoot());
    }

    @SystemLog("获取部门列表")
    @GetMapping("/findList")
    @ResponseBody
    public RestResponse getList(@RequestParam(required = false, defaultValue = "0") Integer parentId) {
        List<SysDept> deptList = deptService.selectByParentId(parentId);
        return RestResponse.success().setCode(0).setData(deptList);
    }

    @SystemLog("调整部门排序")
    @PostMapping("/swap")
    @ResponseBody
    public RestResponse swapSort(Integer currentId, Integer swapId) {
        deptService.swapSort(currentId, swapId);
        return RestResponse.success();
    }

    @RequestMapping("addUI")
    public String addUI(@RequestParam("parentId") Integer parentId, Model model) {
        model.addAttribute("parentId", parentId);
        return prefix + "add";
    }

    /**
     * 修改部门
     *
     * @param sysDept
     * @return
     */
    @PostMapping("add")
    @SystemLog("添加部门")
    @ApiOperation(value = "/add", httpMethod = "POST", notes = "修改部门")
    @ResponseBody
    public RestResponse add(SysDept sysDept) {
        if (sysDept.getSort() == null) {
            return RestResponse.failure("排序值不能为空");
        }

        if (sysDept.getParentId() == null) {
            return RestResponse.failure("上级部门");
        }
        deptService.saveOrUpdateDict(sysDept);
        return RestResponse.success();
    }

    @RequestMapping("editUI")
    public String editUI(@RequestParam("id") Integer id, Model model) {
        SysDept sysDept = deptService.getById(id);
        model.addAttribute("sysDept", sysDept);
        return prefix + "add";
    }

    /**
     * 修改部门
     *
     * @param sysDept
     * @return
     */
    @PostMapping("edit")
    @SystemLog("修改部门")
    @ApiOperation(value = "/edit", httpMethod = "POST", notes = "修改部门")
    @ResponseBody
    public RestResponse edit(SysDept sysDept) {
        if (sysDept.getId() == null || sysDept.getId() == 0) {
            return RestResponse.failure("id不能为空");
        }
        if (sysDept.getSort() == null) {
            return RestResponse.failure("排序值不能为空");
        }

        if (sysDept.getParentId() == null) {
            return RestResponse.failure("上级部门");
        }
        deptService.saveOrUpdateDict(sysDept);
        return RestResponse.success();
    }

    @PostMapping("delete")
    @ResponseBody
    @SystemLog("删除部门")
    @ApiOperation(value = "/delete", httpMethod = "POST", notes = "删除部门")
    public RestResponse delete(@RequestParam(value = "id",required = false)Integer id){
        if(id == null || id == 0 || id == 1){
            return RestResponse.failure("参数错误");
        }
        SysDept user =deptService.getById(id);
        if(user == null){
            return RestResponse.failure("部门不存在");
        }
        deptService.deleteDept(id);
        return RestResponse.success();
    }
}
